package com.lingyun.financialmanagement.service.pojo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingyun.financialmanagement.dao.mapper.*;
import com.lingyun.financialmanagement.dao.pojo.*;
import com.lingyun.financialmanagement.service.OperationLogService;
import com.lingyun.financialmanagement.utils.TokenGetUser;
import com.lingyun.financialmanagement.vo.ErrorCode;
import com.lingyun.financialmanagement.vo.OperationLogVo;
import com.lingyun.financialmanagement.vo.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class OperationLogServiceImpl implements OperationLogService {

    @Autowired
    private OperationLogMapper operationLogMapper;

    @Value("${redis.token.tokenAccountPrefix}")
    private String tokenAccountPrefix;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UsersMapper usersMapper;

    // 设置每次分页查询数量
    private final Integer PAGE_SIZE = 20;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private ComponentPartMapper componentPartMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private BusinessTripMapper businessTripMapper;

    @Override
    public Result selectLogOrderGroup(Integer page) {
        if (page == null || page < 1){
            page = 1;
        }

        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");

        Page<OperationLog> operationLogPage = new Page<>(page, 10);

        Page<OperationLog> operationLogPage1 = operationLogMapper.selectPage(operationLogPage, queryWrapper);

        return Result.success(operationLogPage1);
    }

    @Override
    public Result selectMyUser() {
        String token = request.getHeader("token");
        // token为空
        if (token == null || token.isEmpty()){
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }
        String account = redisTemplate.opsForValue().get(tokenAccountPrefix + token);
        // 获取账号为空
        if (account == null || account.isEmpty()){
            return Result.success(null);
        }
        Users users = usersMapper.selectByAccount(account);
        return Result.success(users);
    }

    @Override
    @Transactional
    public Result insertLog(HttpServletRequest request, OperationLog operationLog) {
        Result usersResult = TokenGetUser.getUsers(request, redisTemplate, tokenAccountPrefix, usersMapper);
        if (usersResult.getCode() != 200){
            return usersResult;
        }
        Users users = (Users) usersResult.getData();
        operationLog.setUsers(users.getId());
        int insert = 0;
        try{
            insert = operationLogMapper.insert(operationLog);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(ErrorCode.LOG_CREATE_ERROR.getCode(), ErrorCode.LOG_CREATE_ERROR.getMsg());
        }
        if (insert == 0){
            return Result.fail(ErrorCode.LOG_CREATE_ERROR.getCode(), ErrorCode.LOG_CREATE_ERROR.getMsg());
        }
        return Result.success(true);
    }

    @Override
    public Result selectLog() {
        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        List<OperationLog> operationLogs = operationLogMapper.selectList(queryWrapper);

        // 日志列表
        ArrayList<OperationLogVo> operationLogVos = new ArrayList<>();
        for (int i = 0; i < operationLogs.size(); i++) {
            // 获取所有姓名与id
            Users users = null;
            if (operationLogs.get(i).getUsers() != null){
                users = usersMapper.selectById(operationLogs.get(i).getUsers());
            }
            // 元件获取
            ComponentPart componentPart = null;
            if (operationLogs.get(i).getComponentPart() != null){
                componentPart = componentPartMapper.selectById(operationLogs.get(i).getComponentPart());
            }
            // 获取项目
            Project project = null;
            if (operationLogs.get(i).getProject() != null){
                project = projectMapper.selectById(operationLogs.get(i).getProject());
            }

            // 差旅获取
            BusinessTrip businessTrip = null;
            if (operationLogs.get(i).getBusinessTrip() != null){
                businessTrip = businessTripMapper.selectById(operationLogs.get(i).getBusinessTrip());
            }

            // 获取用户名
            String userName = null;
            if (users != null){
                userName = users.getName();
            }
            // 元件名称
            String componentPartName = null;
            if (componentPart != null){
                componentPartName = componentPart.getName();
            }
            // 项目名称
            String projectName = null;
            if (project != null){
                projectName = project.getName();
            }
            String businessTripName = null;
            if (businessTrip != null){
                businessTripName = businessTrip.getName();
            }

            OperationLogVo operationLogVo = new OperationLogVo(operationLogs.get(i).getId(), operationLogs.get(i).getUsers(), userName, operationLogs.get(i).getComponentPart(), componentPartName, operationLogs.get(i).getBusinessTrip(), businessTripName, operationLogs.get(i).getProject(), projectName, operationLogs.get(i).getLogText(), operationLogs.get(i).getCreateTime());
            operationLogVos.add(operationLogVo);
        }

        return Result.success(operationLogVos);
    }
}
